﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace JustinAngel.Neurons.Pages.Search
{
    public partial class SearchPage : PhoneApplicationPage
    {
        public SearchPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SearchPage_Loaded);
            LayoutRoot.Background = PanoramaBackground;

            searchTextBox.KeyDown += new KeyEventHandler(searchTextBox_KeyDown);
        }

        void searchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                DoSearch();
            }
        }

        void SearchPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (_wasNavigateToAndNeedsAKeyboardFocus)
            {
                _wasNavigateToAndNeedsAKeyboardFocus = false;
                searchTextBox.Focus();
            }
        }

        private void search_click(object sender, System.EventArgs e)
        {
            DoSearch();
        }

        private void DoSearch()
        {
            SearchViewModel vm = this.DataContext as SearchViewModel;
            if (vm != null)
            {
                searchTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                this.Focus();
                vm.Search.Execute(null);
            }
        }

        private void cancel_click(object sender, System.EventArgs e)
        {
            SearchViewModel vm = this.DataContext as SearchViewModel;
            if (vm != null)
            {
                searchTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                this.Focus();
                vm.Cancel.Execute(null);
            }
        }

        public ImageBrush PanoramaBackground
        {
            get
            {
                return new ImageBrush()
                {
                    ImageSource = new BitmapImage(PanoramaBackgroundImageSource),
                    Stretch = Stretch.None
                };
            }
        }

        public Uri PanoramaBackgroundImageSource
        {
            get
            {
                Visibility PhoneLightThemeVisibility = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
                if (PhoneLightThemeVisibility == Visibility.Collapsed)
                {
                    return new Uri("/PanoramaBackgroundNeuron.jpg", UriKind.Relative);
                }
                else
                {
                    return new Uri("/PanoramaBackgroundNeuronLight.jpg", UriKind.Relative);
                }
            }
        }

        private bool _wasNavigateToAndNeedsAKeyboardFocus = false;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            _wasNavigateToAndNeedsAKeyboardFocus = true;
        }

    }
}